package com.suse.warmstill.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.suse.warmstill.aspect.powercheck.LimitRequestAnno;
import com.suse.warmstill.common.enums.ResponseEnum;
import com.suse.warmstill.common.exception.BusinessException;
import com.suse.warmstill.common.model.CallResult;
import com.suse.warmstill.common.model.ListPageModel;
import com.suse.warmstill.common.utils.UserIdThreadLocal;
import com.suse.warmstill.mapper.ChatMsgMapper;
import com.suse.warmstill.service.ChatMsgService;
import com.suse.warmstill.service.UserSefvice;
import com.suse.warmstill.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;


/**
 * @Author 黄靖
 * @Data 2022/9/29 13:02
 * Describe: 用户处理和登入处理
 */
@CrossOrigin("")
@RestController
@RequestMapping("/warmstill/user")
@Api("登入管理")
public class PCUserApi {


    @Resource
    private ChatMsgService chatMsgService;

    @Autowired
    UserSefvice userSefvice;

    //获取验证码
    @LimitRequestAnno()
    @ApiOperationSupport(order = 10)
    @ApiOperation("获取验证码图片")
    @GetMapping("/getLoginImageCode")
    public void getLoginImageCode(@RequestParam(value = "uuid", required = true) String uuid,
                                  ServletResponse response
    ) throws IOException {

        userSefvice.getLoginImageCode(uuid, response);
    }
    /*
     * 校验验证码
     * */
  /*  @ApiOperation("校验验证码")
    @ApiOperationSupport(order = 20)
    @PostMapping("/verifyLoginImageCode")
    public CallResult verifyLoginImageCode(@RequestBody @Validated CapchaVo capchaVo){
        userSefvice.verifyLoginImageCode(capchaVo.getUuid(),capchaVo.getCode());
        return CallResult.success();
    }*/

    @ApiOperationSupport(order = 30)
    @ApiOperation("登入,返回 userID,以及userName")
    @PostMapping("/user-tokens")
    public CallResult login(@RequestBody @Validated LoginVo loginVo, HttpServletResponse response, HttpServletRequest request) {
        final Map loginMap = userSefvice.login(loginVo, response, request);
        return CallResult.success(loginMap);
    }

    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    @ApiOperation("新增 用户,同时指定 角色")
    @PostMapping("/insertUser")
    public CallResult<Void> insertUser(@RequestBody @Validated UserPostVo userVo) {
        userSefvice.inserUser(userVo);
        return CallResult.success();
    }

    @ApiOperation("复合分页查询,查询出 角色 资源")
    @GetMapping("/listRoles")
    public CallResult<ListPageModel<UserVo>> listUsers(@Validated ConditionVO conditionVO) {

        ListPageModel<UserVo> list = userSefvice.listUsers(conditionVO);
        return CallResult.success(list);
    }

    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    @ApiModelProperty("逻辑删除用户")
    @PutMapping("/deleteUser/{userId}")
    public CallResult<Void> LogicDeleteUser(@PathVariable("userId") Long userId) {
        if (ObjectUtils.isEmpty(userId)) {
            throw new BusinessException(ResponseEnum.PARAM_ISNULL.getCode(), ResponseEnum.PARAM_ISNULL.getMsg());
        }
        userSefvice.LogicDeleteUser(userId);
        return CallResult.success();
    }

    @ApiOperation("禁用账户,status =0的时候，是启用， =1的时候 是禁用账号")
    @GetMapping("/foribddenUser/{userId},{status}")
    public CallResult<Void> foribddenUser(@PathVariable("userId") Long userId, @PathVariable("status") int status) {

        userSefvice.foribddenUser(userId, status);
        return CallResult.success();
    }

    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    @ApiOperation("退出账户")
    @GetMapping("/logoutUser/{userId}")
    public CallResult logoutUser(@PathVariable("userId") Long userId, HttpServletRequest request, HttpServletResponse response) {
        final Long aLong = UserIdThreadLocal.get();

        userSefvice.logoutUser(aLong, request, response);
        return CallResult.success();
    }


}
